package com.citcc.web.controller.statistics;

import com.citcc.business.bean.CdzStation;
import com.citcc.business.bean.UserOrderEntity;
import com.citcc.business.bean.vo.StatisticsStakeVo;
import com.citcc.business.service.CdzStationService;
import com.citcc.business.service.UserOrderEntityService;
import com.citcc.common.annotation.Log;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.enums.BusinessType;
import com.citcc.common.page.TableDataInfo;
import com.citcc.common.utils.StringUtils;
import com.citcc.common.utils.poi.ExcelUtil;
import com.citcc.framework.web.base.BaseController;
import com.citcc.web.controller.util.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 单桩统计
 */
@Controller
@RequestMapping("/statistics/stake")
public class StakeController extends BaseController
{
    private String prefix = "statistics";

    @Autowired
    private UserOrderEntityService userOrderEntityService;

    @Autowired
    private CdzStationService cdzStationService;

    /**
     * 跳转单桩页面
     * @return
     */
    @RequiresPermissions("statistics:stake:view")
    @GetMapping()
    public String stake(ModelMap map)
    {
        CdzStation station = new CdzStation();
        station.setOrgId(getSysUser().getDeptId());
        map.put("stations",cdzStationService.selectCdzStationAll(station));
        LocalDate date = LocalDate.now();
        map.put("endTime",date.toString());
        date = date.minusMonths(1);
        map.put("beginTime",date.toString());
        return prefix + "/stakeList";
    }

    /**
     * 单桩列表
     * @param beginTime
     * @param endTime
     * @param stationCodes
     * @return
     */
    @RequiresPermissions("statistics:stake:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(String beginTime, String endTime, String stationCodes)
    {
        startPage();
        //如果时间为空  默认最近一个月时间
        if (StringUtils.isEmpty(beginTime) && StringUtils.isEmpty(endTime)){
            LocalDate date = LocalDate.now();
            endTime = date.toString();
            date = date.minusMonths(1);
            beginTime = date.toString();
        }
        List<StatisticsStakeVo> list = userOrderEntityService.getStakeList(beginTime, endTime, stationCodes,getSysUser().getDeptId());
        return getDataTable(list);
    }

    /**
     * 单桩列表导出
     * @param beginTime
     * @param endTime
     * @param stationCodes
     * @return
     */
    @Log(title = "单桩统计报表", businessType = BusinessType.EXPORT)
    @RequiresPermissions("statistics:stake:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(String beginTime, String endTime, String stationCodes)
    {
        //如果时间为空  默认最近一个月时间
        if (StringUtils.isEmpty(beginTime) && StringUtils.isEmpty(endTime)){
            LocalDate date = LocalDate.now();
            endTime = date.toString();
            date = date.minusMonths(1);
            beginTime = date.toString();
        }
        List<StatisticsStakeVo> list = userOrderEntityService.getStakeList(beginTime, endTime, stationCodes,getSysUser().getDeptId());
        ExcelUtil<StatisticsStakeVo> util = new ExcelUtil<>(StatisticsStakeVo.class);
        LocalDateTime localDateTime = LocalDateTime.now();
        String dateTime = DateUtil.getYmdhms(localDateTime);
        return util.exportExcel(list, dateTime + "单桩统计报表");
    }

    /**
     * 跳转单桩明细列表
     * @param stakeCode
     * @param mmap
     * @return
     */
    @RequiresPermissions("statistics:stake:detail")
    @GetMapping("/detail/{stakeCode},{beginTime},{endTime}")
    public String detail(@PathVariable("stakeCode") String stakeCode,
                         @PathVariable("beginTime") String beginTime,
                         @PathVariable("endTime") String endTime,ModelMap mmap)
    {
        mmap.put("stakeCode", stakeCode);
        mmap.put("beginTime", beginTime);
        mmap.put("endTime", endTime);
        return prefix + "/stakeDetail";
    }

    /**
     * 单桩明细列表
     * @param userOrderEntity
     * @return
     */
    @RequiresPermissions("statistics:stake:listDetail")
    @PostMapping("/listDetail")
    @ResponseBody
    public TableDataInfo listDetail(UserOrderEntity userOrderEntity)
    {
        startPage();
        List<UserOrderEntity> list = userOrderEntityService.getStakeDetail(userOrderEntity);
        return getDataTable(list);
    }

    /**
     * 单桩明细列表导出
     * @param userOrderEntity
     * @return
     */
    @Log(title = "单桩统计报表明细", businessType = BusinessType.EXPORT)
    @RequiresPermissions("statistics:stake:exportDetail")
    @PostMapping("/exportDetail")
    @ResponseBody
    public AjaxResult exportDetail(UserOrderEntity userOrderEntity)
    {
        List<UserOrderEntity> list = userOrderEntityService.getStakeDetail(userOrderEntity);
        ExcelUtil<UserOrderEntity> util = new ExcelUtil<>(UserOrderEntity.class);
        LocalDateTime localDateTime = LocalDateTime.now();
        String dateTime = DateUtil.getYmdhms(localDateTime);
        return util.exportExcel(list, dateTime + "单桩统计报表明细");
    }

}